{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TensorFlow"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "TensorFlow is an open source software library by Google which is extensively used for\n",
    "numerical computation. It uses data flow graphs which can be shared and executed on\n",
    "many different platforms. \n",
    "\n",
    "It is widely used for building deep learning models which is a\n",
    "subset of machine learning. Tensor is nothing but a multidimensional array, so when we\n",
    "say tensorflow ,it is literally a flow of multi-dimensional arrays (tensor) in the computation\n",
    "graph.\n",
    "With Anaconda installed, installing tensorflow has become very simple. Irrespective of the\n",
    "platform you are using, you can easily install tensorflow by typing the following command.\n",
    "\n",
    "\n",
    "<br>\n",
    "source activate universe\n",
    "\n",
    "conda install -c conda-forge tensorflow"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " We can check successful tensorflow installation by simply running the following hello\n",
    "world program.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'Hello World'\n"
     ]
    }
   ],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import tensorflow as tf\n",
    "hello = tf.constant(\"Hello World\")\n",
    "sess = tf.Session()\n",
    "print(sess.run(hello))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Variables, Constants, and Placeholders\n",
    "\n",
    "\n",
    "Variables, constants, placeholders are the fundamental elements of tensorflow. However,\n",
    "there is always a confusion between these three. Let us see each element one by one and\n",
    "learn the difference between them."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### Variables\n",
    " Variables are the containers used to store values. Variables will be used as input to several\n",
    "other operations in the computation graph. We can create tensorflow variables using\n",
    "tf.Variable() function. In the below example, we define a variable with values from a\n",
    "random normal distribution and name it as weights.\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "weights = tf.Variable(tf.random_normal([3, 2], stddev=0.1), name=\"weights\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " However, after defining a variable, we need to explicitly create an initialization operation\n",
    "using tf.global_variables_initializer() method which will allocate resources for\n",
    "the variable."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Constants\n",
    "\n",
    " Constants, unlike variables, their values cannot be changed. Constant are immutable once\n",
    "they are assigned values they cannot be changed throughout. we can create constant using\n",
    "tf.constant() function.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = tf.constant(13)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Placeholders\n",
    "\n",
    "Think of placeholders as a variable where you only define the type and dimension but will\n",
    "not assign the value. Placeholders are defined with no values. values for the placeholders\n",
    "will be fed at the runtime. Placeholders have an optional argument called shape which\n",
    "specifies the dimensions of the data. If the shape is set to none then we can feed data of any\n",
    "size at runtime. Placeholders can be defined using tf.placeholder() function\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = tf.placeholder(\"float\", shape=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To put in simple terms, we use tf.variable to store the data and tf.placeholder for\n",
    "feeding the external data."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Computation Graph\n",
    "\n",
    " Everything in tensorflow will be represented as a computation graph which consists of\n",
    "nodes and edges, where nodes are the mathematical operations, say addition,\n",
    "multiplication etc.. and edges are the tensors. Having computational graph is very efficient\n",
    "in optimizing resources and it also promotes distributed computing.\n",
    "\n",
    "Say we have node B whose input is dependent on the output of node A this type of\n",
    "dependency is called direct dependency."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "A = tf.multiply(8,5)\n",
    "B = tf.multiply(A,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When node B doesn't depend on node A for its input it is called indirect dependency"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "A = tf.multiply(8,5)\n",
    "B = tf.multiply(4,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " So if we can understand these dependencies, we can distribute the independent\n",
    "computations in available resources and reduce the computation time.\n",
    "Whenever we import tensorflow, automatically default graph will be created and all nodes\n",
    "we create will get associated with the default graph."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sessions\n",
    "\n",
    "Computation graphs will only be defined, in order to execute the computation graph we\n",
    "use tensorflow sessions.\n",
    "sess = tf.Session()\n",
    "We can create the session for our computation graph using tf.Session() method which\n",
    "will allocate the memory for storing the current value of the variable. After creating the\n",
    "session, we can execute our graph using sess.run() method.\n",
    "In order to run anything in tensorflow, we need to start the tensorflow session for an\n",
    "instance, look at the below code,\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"Mul_4:0\", shape=(), dtype=int32)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "a = tf.multiply(2,3)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It will print tensorflow object instead of 6. Because as already said, whenever we import\n",
    "tensorflow a default computation graph will be automatically created and all nodes which\n",
    "we create will get attached to the graph. In order to execute the graph, we need to\n",
    "initialize tensorflow session as follows,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "a = tf.multiply(2,3)\n",
    "\n",
    "#create tensorflow session for executing the session\n",
    "with tf.Session() as sess:\n",
    " #run the session\n",
    " print(sess.run(a))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:universe]",
   "language": "python",
   "name": "conda-env-universe-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
